package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketBrand;
import com.market.bean.MarketBrandExample;
import com.market.bean.bo.admin.AdminBrandUpdateBo;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.bean.validation.ValidationBrand;
import com.market.bean.vo.admin.AdminBrandCreateVo;
import com.market.mapper.MarketBrandMapper;
import com.market.service.admin.AdminBrandService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author:WalterChan
 * @Decription:AdminBrandServiceImpl
 * @Date Created in 2022-12-29-22:10
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;


    @Override
    public CommonData<MarketBrand> list(BasePageInfo info, String id, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketBrandExample example = new MarketBrandExample();
        example.setOrderByClause(info.getSort()+" "+info.getOrder());

        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }

        if (!StringUtil.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketBrand> list = marketBrandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);


    }

    @Override
    public AdminBrandCreateVo create(ValidationBrand brand) {

        //新增需要插入的数据
        MarketBrand insertMarketBrand = new MarketBrand();
//        从参数中对插入的数据进行赋值
        Date date = new Date();
        insertMarketBrand.setFloorPrice(brand.getFloorPrice());
        insertMarketBrand.setPicUrl(brand.getPicUrl());
        insertMarketBrand.setName(brand.getName());
        insertMarketBrand.setDesc(brand.getDesc());
        insertMarketBrand.setUpdateTime(date);
        insertMarketBrand.setAddTime(date);

//        插入数据
        marketBrandMapper.insertSelective(insertMarketBrand);

//        通过返回的ID搜索到返回的刚才插入的数据
        MarketBrand result = marketBrandMapper.selectByPrimaryKey(insertMarketBrand.getId());

//        将搜索的结果封装起来返回
        AdminBrandCreateVo adminBrandCreateVo = new AdminBrandCreateVo();
        adminBrandCreateVo.setAddTime(result.getAddTime());
        adminBrandCreateVo.setDesc(result.getDesc());
        adminBrandCreateVo.setFloorPrice(result.getFloorPrice());
        adminBrandCreateVo.setId(result.getId());
        adminBrandCreateVo.setName(result.getName());
        adminBrandCreateVo.setPicUrl(result.getPicUrl());
        adminBrandCreateVo.setUpdateTime(result.getUpdateTime());


        return adminBrandCreateVo;

    }

    @Override
    public AdminBrandCreateVo update(AdminBrandUpdateBo updateBo) {
//       获取参数数据
        MarketBrand updateMarketBrand = new MarketBrand();
        updateMarketBrand.setAddTime(updateBo.getAddTime());
        updateMarketBrand.setDesc(updateBo.getDesc());
        updateMarketBrand.setFloorPrice(updateBo.getFloorPrice());
        updateMarketBrand.setId(updateBo.getId());
        updateMarketBrand.setName(updateBo.getName());
        updateMarketBrand.setPicUrl(updateBo.getPicUrl());
        updateMarketBrand.setUpdateTime(updateBo.getUpdateTime());

        marketBrandMapper.updateByPrimaryKeySelective(updateMarketBrand);

        //        通过ID搜索到返回的刚才更新的数据
        MarketBrand result = marketBrandMapper.selectByPrimaryKey(updateBo.getId());

//        将搜索的结果封装起来返回
        AdminBrandCreateVo adminBrandCreateVo = new AdminBrandCreateVo();
        adminBrandCreateVo.setAddTime(result.getAddTime());
        adminBrandCreateVo.setDesc(result.getDesc());
        adminBrandCreateVo.setFloorPrice(result.getFloorPrice());
        adminBrandCreateVo.setId(result.getId());
        adminBrandCreateVo.setName(result.getName());
        adminBrandCreateVo.setPicUrl(result.getPicUrl());
        adminBrandCreateVo.setUpdateTime(result.getUpdateTime());


        return adminBrandCreateVo;




//        进行更新
//        返回值
    }

    @Override
    public void delete(MarketBrand marketBrand) {

        marketBrand.setDeleted(true);
        int reslut = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);

        return;

    }
}
